import os

import pandas as pd
from excutor.excutor import Excutor

from prettify import prettify_data
from fileReader.excelReader import ExcelReader
from fileReader.csvReader import CsvReader

## 源文件路径
originFilePath = r'测试excel\12-2号value_mapping.xlsx'
## 目标文件路径
dstFilePath = r'测试excel\test.csv'

## 自动导入Excutor类
excutorList = os.listdir('excutor')
excutors = [exe[:-3] for exe in excutorList if 'Excutor.py' in exe]
classexeNames = [cls[0].upper() + cls[1:-7] for cls in excutors]
for module,cls in zip(excutors,classexeNames):
    exec('from excutor.{} import {}'.format(module,cls))
ExcutorList = [eval(name) for name in classexeNames]

## 一些翻译
trans = {
    'Capacity':'电容',
    'Current':'电流',
    'Frequency':'频率',
    'Inductance':'电感',
    'Package':'包装',
    'Power':'功率',
    'Resistance':'电阻',
    'Size':'尺寸',
    'Temperature':'温度',
    'Voltage':'电压'
}


## 工厂函数，传入Excutor类的名字，返回一个函数
def func_fact_Exe(className:Excutor):
    def func(data):
        obj = className(data)
        return obj.processingData()
    return func



def main():
    if ExcelReader.isExcel(originFilePath):
        reader = ExcelReader(originFilePath)
    elif CsvReader.isCsv(originFilePath):
        reader = ExcelReader(originFilePath)
    else:
        print(originFilePath + ' 文件格式错误')

    data = reader.read()

    for excutor in ExcutorList:
        exeFunc = func_fact_Exe(excutor)

        if excutor.Existence(data):
            data = exeFunc(data)
            print(f'{trans[excutor.__name__]}数据修改完毕')
        else:
            print(f'没有{trans[excutor.__name__]}数据')
    
    data = prettify_data(data)

    data.to_csv(dstFilePath,index=False)


if __name__ == '__main__':
    main()